package com.stockz.core.util;

import com.stockz.core.model.Bar;

public abstract class BarUtil {

	public static enum BarValueType{
		OPEN("Open"), HIGH("High"), LOW("Low"), CLOSE("Close"), LAST("Last"), VOLUME("Volume"), 
		TURNOVER("Turnover"), TRADES("Trades"), AVGPRICE("Average Price"), 
		AVGTRADESIZE("Average Trade Size"), MID("Mid"),SPREAD("Spread");
		
		private BarValueType(String value) {
			this.value = value;
		}
		
		private final String value;
		
		public String toString(){
			return value;
		}
	};
	
	public static double getValue(Bar bar, BarValueType barValueType){
		if(bar != null && barValueType != null){
			switch(barValueType){
			case AVGPRICE : return bar.getVolume() == 0?bar.getClose():bar.getTurnover()/bar.getVolume();
			case AVGTRADESIZE : return bar.getTrades() == 0?0:bar.getVolume()/bar.getTrades();
			case CLOSE : return bar.getClose();
			case HIGH : return bar.getHigh();
			case LAST : return bar.getLast();
			case LOW : return bar.getLow();
			case MID : return (bar.getHigh()+bar.getLow())/2;
			case OPEN : return bar.getOpen();
			case TRADES : return bar.getTrades();
			case TURNOVER : return bar.getTurnover();
			case VOLUME : return bar.getVolume();
			case SPREAD : return bar.getHigh() - bar.getLow();
			}
		}
		return 0;
	}
	
	
	public static void copy(Bar from, Bar to){
		if(from.getClose() != 0) to.setClose(from.getClose());
		if(from.getCode() != null) to.setCode(from.getCode());
		if(from.getDate() != null) to.setDate(from.getDate());
		if(from.getHigh() != 0) to.setHigh(from.getHigh());
		if(from.getLast() != 0) to.setLast(from.getLast());
		if(from.getLow() != 0) to.setLow(from.getLow());
		if(from.getOpen() != 0) to.setOpen(from.getOpen());
		if(from.getPreviousClose() != 0) to.setPreviousClose(from.getPreviousClose());
		if(from.getTrades() != 0) to.setTrades(from.getTrades());
		if(from.getTurnover() != 0) to.setTurnover(from.getTurnover());
		if(from.getVolume() != 0) to.setVolume(from.getVolume());
	}
	
}
